package com.example.service.impl;

import com.example.dao.TbItemParamMapper;
import com.example.dto.EasyUIDataGridResult;
import com.example.dto.Result;
import com.example.entity.TbItemParam;
import com.example.service.ItemParamService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * <p>商品规格参数模版管理</p>
 * Created by zhezhiyong@163.com on 2018/7/30.
 */
@Service
public class ItemParamServiceImpl implements ItemParamService {

    @Resource
    private TbItemParamMapper tbItemParamMapper;

    @Override
    public Result getItemParamByCid(Long cid) {
        Example example = new Example(TbItemParam.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("itemCatId", cid);
        List<TbItemParam> tbItemParamList = tbItemParamMapper.selectByExample(example);
        return tbItemParamList != null && tbItemParamList.size() > 0 ? Result.ok(tbItemParamList.get(0)) : Result.ok();
    }

    @Override
    public Result saveItemParam(Long cid, String paramData) {
        TbItemParam itemParam = new TbItemParam();
        itemParam.setItemCatId(cid);
        itemParam.setParamData(paramData);
        itemParam.setCreated(new Date());
        itemParam.setUpdated(new Date());
        tbItemParamMapper.insert(itemParam);
        return Result.ok();
    }

    @Override
    public EasyUIDataGridResult getItemParamList(Integer page, Integer rows) {
        PageHelper.startPage(page, rows);
        Example example = new Example(TbItemParam.class);
        List<TbItemParam> itemParamList = tbItemParamMapper.selectByExample(example);
        EasyUIDataGridResult result = new EasyUIDataGridResult();
        PageInfo<TbItemParam> pageInfo = new PageInfo<>(itemParamList);
        result.setTotal(pageInfo.getTotal());
        result.setRows(itemParamList);
        return result;
    }


}
